package com.yunxi.controller.sto;

import com.aliyuncs.utils.StringUtils;
import com.yunxi.core.base.WebLog;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.VehicleStockDubboService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 经销商车辆库存 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-29
 */
@RestController
@RequestMapping("/admin/vehicleStock")
@Api(tags = "sto:vehicleStock", description = "经销商车辆库存接口")
public class VehicleStockController {
    private static final Logger logger = LoggerFactory.getLogger(VehicleStockController.class);

    @Autowired
    VehicleStockDubboService vehicleStockDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    @WebLog(description = "车辆库存管理分页查询")
    public ApiResult page(VehicleStockQueryVO vehicleStockQueryVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
    if(vehicleStockQueryVO.getIsQueryPartner()==null){
     vehicleStockQueryVO.setPartnerCode(loginInfo.getPartnerCode());
    }
     logger.debug("==============================车辆库存查询c:"+vehicleStockQueryVO.getPartnerCode()+"===========");
     //vehicleStockQueryVO.setStorageStatus("1");
     return ResponseUtil.ok(vehicleStockDubboService.findPageByVO(vehicleStockQueryVO,pageNum,pageSize));
    }

    @PostMapping("/frozen")
    @ApiOperation(value = "车辆冻结")
    public ApiResult frozen(@RequestBody @ApiParam(value = "主键id集合") List<VehicleStockFrozenVO> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockDubboService.frozenAndReset(list,"1",null,loginInfo.getUserCode());
     return ResponseUtil.ok("success");
    }

    @PostMapping("/reset")
    @ApiOperation(value = "车辆解冻")
    public ApiResult reset(@RequestBody @ApiParam(value = "主键id集合") List<VehicleStockFrozenVO> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockDubboService.frozenAndReset(list,"0",null,loginInfo.getUserCode());
     return ResponseUtil.ok("success");
    }

    @PostMapping("/repairOut")
    @ApiOperation(value = "维修出库")
    public ApiResult repairOut(@RequestBody @ApiParam(value = "主键id集合") List<String> list) throws Exception {
     vehicleStockDubboService.repairInOut(list,1);
     return ResponseUtil.ok("success");
    }

    @PostMapping("/repairIn")
    @ApiOperation(value = "维修入库")
    public ApiResult repairIn(@RequestBody @ApiParam(value = "主键id集合") List<String> list) throws Exception {
     vehicleStockDubboService.repairInOut(list,0);
     return ResponseUtil.ok("success");
    }

    @PostMapping("/maintainLoad")
    @ApiOperation(value = "根据vehicleId查看厂端库存车辆详情")
    public ApiResult maintainLoad(String vehicleId) throws Exception {
     return ResponseUtil.ok(vehicleStockDubboService.findByVehicleId(vehicleId));
    }

    @PostMapping("/maintainSave")
    @ApiOperation(value = "保存保养")
    public ApiResult maintainSave(@RequestBody VehicleStockVO vehicleStockVO, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockDubboService.saveOrUpdate(vehicleStockVO);
     return ResponseUtil.ok("success");
    }

    @GetMapping("/mainTainPage")
    @ApiOperation(value = "当前厂端库存车辆保养分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")})
    public ApiResult mainTainPage(VehicleStockQueryVO vehicleStockQueryVO, Integer pageNum, Integer pageSize) throws Exception {
     return ResponseUtil.ok(vehicleStockDubboService.findMaintainPageByVO(vehicleStockQueryVO,pageNum,pageSize));
    }

    @GetMapping("/mainTainExport")
    @ApiOperation(value = "当前厂端库存车辆保养导出")
    public ApiResult mainTainExport(VehicleStockQueryVO vehicleStockQueryVO) throws Exception {
     return ResponseUtil.ok(vehicleStockDubboService.mainTainExport(vehicleStockQueryVO));
    }

    @GetMapping("/seriesList")
    @ApiOperation(value = "查询车系列表")
    public ApiResult seriesList(@CurrentUser LoginInfo loginInfo) throws Exception {
     return ResponseUtil.ok(vehicleStockDubboService.seriesList());
    }

    @GetMapping("/catalogList")
    @ApiOperation(value = "查询配置列表")
    public ApiResult catalogList(@CurrentUser LoginInfo loginInfo) throws Exception {
     return ResponseUtil.ok(vehicleStockDubboService.catalogList());
    }

    @GetMapping("/export")
    @ApiOperation(value = "厂端车辆库存导出")
    public ApiResult export(VehicleStockQueryVO vehicleStockQueryVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vehicleStockQueryVO.setStorageStatus("1");
     return ResponseUtil.ok(vehicleStockDubboService.export(vehicleStockQueryVO));
    }

    @PostMapping("/importFrozen")
    @ApiOperation(value = "导入批量冻结/解冻")
    public ApiResult importFrozen(@RequestBody VehicleStockImportVO vehicleStockImportVO, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockDubboService.importFrozen(vehicleStockImportVO,loginInfo.getUserCode());
     return ResponseUtil.ok("success");
    }

    @GetMapping("/sharePage")
    @ApiOperation(value = "体验车查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult sharePage(VehicleStockQueryVO vehicleStockQueryVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockQueryVO.setPartnerCode(loginInfo.getPartnerCode());
     return ResponseUtil.ok(vehicleStockDubboService.sharePage(vehicleStockQueryVO,pageNum,pageSize));
    }

    @PostMapping("/addShare")
    @ApiOperation(value = "批量添加共享资源")
    public ApiResult addShare(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockDubboService.setShare(list,loginInfo.getUserCode(),1);
     return ResponseUtil.ok("success");
    }

    @PostMapping("/cancelShare")
    @ApiOperation(value = "批量取消共享资源")
    public ApiResult cancelShare(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockDubboService.setShare(list,loginInfo.getUserCode(),0);
     return ResponseUtil.ok("success");
    }

    @GetMapping("/shareList")
    @ApiOperation(value = "共享车查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult shareList(VehicleStockQueryVO vehicleStockQueryVO, Integer pageNum, Integer pageSize, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockQueryVO.setPartnerCode(loginInfo.getPartnerCode());
     return ResponseUtil.ok(vehicleStockDubboService.shareList(vehicleStockQueryVO,pageNum,pageSize));
    }

    @GetMapping("/financePage")
    @ApiOperation(value = "畅想车查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult financePage(VehicleStockQueryVO vehicleStockQueryVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockQueryVO.setPartnerCode(loginInfo.getPartnerCode());
     return ResponseUtil.ok(vehicleStockDubboService.financePage(vehicleStockQueryVO,pageNum,pageSize));
    }

    @GetMapping("/exportFrozenHis")
    @ApiOperation(value = "导出车辆冻结明细")
    public ApiResult exportFrozenHis() throws Exception {
     return ResponseUtil.ok(vehicleStockDubboService.exportFrozenList());
    }

    @PostMapping("/stockReport")
    @ApiOperation(value = "库存报表")
    public ApiResult stockReport(@RequestBody VehicleStockReportVO vehicleStockReportVO, @CurrentUser LoginInfo loginInfo) throws Exception {
     vehicleStockReportVO.setPartnerCode(loginInfo.getPartnerCode());
     return ResponseUtil.ok(vehicleStockDubboService.stockReport(vehicleStockReportVO));
    }

    @PostMapping("/stockReportDetail")
    @ApiOperation(value = "库存报表详情")
    public ApiResult stockReportDetail(@RequestBody VehicleStockReportDetailVO vehicleStockReportVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        vehicleStockReportVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(vehicleStockDubboService.stockReportDetail(vehicleStockReportVO));
    }

    @GetMapping("/newSharePage")
    @ApiOperation(value = "体验车管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult newSharePage(VehicleStockQueryVO vehicleStockQueryVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        vehicleStockQueryVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(vehicleStockDubboService.newSharePage(vehicleStockQueryVO,pageNum,pageSize));
    }

    @PostMapping("/vehicleBind")
    @ApiOperation(value = "体验车绑定")
    public ApiResult vehicleBind(@RequestBody VehicleStockBindVO vehicleStockBindVO) throws Exception {
        vehicleStockDubboService.vehicleBind(vehicleStockBindVO);
        return ResponseUtil.ok();
    }

    @PostMapping("/testDriveOut")
    @ApiOperation(value = "试驾车出库")
    public ApiResult testDriveOut(@RequestBody VehicleStockBindVO vehicleStockBindVO) throws Exception {
        vehicleStockDubboService.testDriveOut(vehicleStockBindVO);
        return ResponseUtil.ok();
    }

    @GetMapping("/mainTainList")
    @ApiOperation(value = "当前厂端库存车辆保养列表")
    public ApiResult mainTainList(VehicleStockQueryVO vehicleStockQueryVO) throws Exception {
        return ResponseUtil.ok(vehicleStockDubboService.findMaintainList(vehicleStockQueryVO));
    }

    @GetMapping("/partnerExport")
    @ApiOperation(value = "店端车辆库存导出")
    public ApiResult partnerExport(VehicleStockQueryVO vehicleStockQueryVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        if(StringUtils.isEmpty(vehicleStockQueryVO.getPartnerCode())){
            vehicleStockQueryVO.setPartnerCode(loginInfo.getPartnerCode());
        }
        return ResponseUtil.ok(vehicleStockDubboService.export(vehicleStockQueryVO));
    }
}
